package com.youkeyi.ddy.cloud.basic.pub.pay.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.pub.pay.entity.SysPayType;
import com.youkeyi.ddy.cloud.basic.pub.pay.service.SysPayTypeService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.pay.PublicPayTypeCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.pay.dto.PublicPayTypeListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.pay.dto.PublicPayTypeListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.pay.param.PublicPayTypeCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.pay.param.PublicPayTypeDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.pay.param.PublicPayTypeQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.pay.param.PublicPayTypeUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 支付方式 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-04-17
 */
@Service
public class SysPayTypeController implements PublicPayTypeCloudService {
    @Autowired
    private SysPayTypeService sysPayTypeService;

    @Override
    public PublicPayTypeListCloudDto query(PublicPayTypeQueryCloudParam param) {
        PublicPayTypeListCloudDto dto = new PublicPayTypeListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getPayTypePrimaryKeys())) {
            param.getPayTypePrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCodes())) {
            param.getCodes().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysPayType> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getPayTypePrimaryKeys()), SysPayType::getPrimaryKey, param.getPayTypePrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), SysPayType::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getCodes()), SysPayType::getCode, param.getCodes())
                .like(StringUtils.isNotBlank(param.getNameFuzzy()), SysPayType::getName, param.getNameFuzzy())
                .ne(SysPayType::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<SysPayType> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysPayType> annexTypeList = sysPayTypeService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(annexTypeList.getRecords())) {
            dto.setTotal(annexTypeList.getTotal());

            List<PublicPayTypeListItemCloudDto> items = new ArrayList<>();
            annexTypeList.getRecords().forEach(annexType -> {
                PublicPayTypeListItemCloudDto item = new PublicPayTypeListItemCloudDto();
                item.setPayTypePrimaryKey(annexType.getPrimaryKey());
                item.setCorpKey(annexType.getCorpKey());
                item.setCode(annexType.getCode());
                item.setName(annexType.getName());
                item.setSort(annexType.getSort());
                item.setIsDeleted(annexType.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(annexType.getCreateTime() != null ? annexType.getCreateTime().getTime() : null);
                item.setCreateUserKey(annexType.getCreateUserKey());
                item.setCreateUserName(annexType.getCreateUserName());
                item.setUpdateUserKey(annexType.getUpdateUserKey());
                item.setUpdateUserName(annexType.getUpdateUserName());
                item.setUpdateTime(annexType.getUpdateTime() != null ? annexType.getUpdateTime().getTime() : null);
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(PublicPayTypeCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getName())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysPayType annexType = new SysPayType();
        annexType.setPrimaryKey(SimpleUUidUtils.getUuid());
        annexType.setCorpKey(param.getCorpKey());
        annexType.setCode(param.getCode());
        annexType.setName(param.getName());
        annexType.setSort(param.getSort());
        annexType.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        annexType.setCreateUserKey(param.getCreateUserKey());
        annexType.setCreateUserName(param.getCreateUserName());
        annexType.setCreateTime(new Date());
        boolean save = sysPayTypeService.save(annexType);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(annexType.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(PublicPayTypeUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getPayTypePrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<SysPayType> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysPayType::getPrimaryKey, param.getPayTypePrimaryKey());
        SysPayType annexType = new SysPayType();
        annexType.setPrimaryKey(param.getPayTypePrimaryKey());
        annexType.setName(param.getName());
        annexType.setSort(param.getSort());
        annexType.setUpdateUserKey(param.getUpdateUserKey());
        annexType.setUpdateUserName(param.getUpdateUserName());
        annexType.setUpdateTime(new Date());
        boolean save = sysPayTypeService.update(annexType, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(PublicPayTypeDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getPayTypePrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysPayType> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysPayType::getPrimaryKey, param.getPayTypePrimaryKeys());

        SysPayType annexType = new SysPayType();
        annexType.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        annexType.setUpdateUserKey(param.getUpdateUserKey());
        annexType.setUpdateUserName(param.getUpdateUserName());
        annexType.setUpdateTime(new Date());
        boolean save = sysPayTypeService.update(annexType, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
